package com.bamboowind.consumer.work;

import com.bamboowind.util.RabbitMqUtils;
import com.bamboowind.util.SleepUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;

/**
 * @Author cpw
 * @Date 2022/10/31 14:47
 */
public class Worker02 {
    public static final String QUEUE_NAME = "work_queue";

    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMqUtils.getChannel();

        //channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        System.out.println("C2等待接收消息处理时间较长");

        // 当消息被传递时通知的信息
        DeliverCallback deliverCallback = (consumerTag, delivery) -> {
            String message = new String(delivery.getBody(), "UTF-8");
            SleepUtils.sleep(30);
            System.out.println("接收到信息:" + message);

            // 手动应答,false表示只应答接收到的那个传递信息,true为应答所有消息
            channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
        };

        int prefetchCount = 1;
        channel.basicQos(prefetchCount);

        boolean flag = false;
        channel.basicConsume(QUEUE_NAME, flag, deliverCallback, consumerTag -> {
            System.out.println(consumerTag + "消费者取消消费接口回调逻辑");
        });
    }
}
